layui.use(['table', 'admin', 'ax','laydate','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var form = layui.form;

    laydate.render({
      elem: '#startTime'
        	,value:new Date()
    });

    laydate.render({
      elem: '#endTime'
        	,value:new Date()
    });

    /**
     * 调度单表管理
     */
    var TDispatch = {
        tableId: "tDispatchTable"
    };
    
    /**
     * 初始化表格的列
     */
    TDispatch.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: '', title: '序号', type:'numbers', width: 120},
            {field: 'chengyunshang', title: '承运商', style:'font-weight: 600;', totalRowText: '合计：'},
            {field: 'qiandaocheci', title: '签到车次', sort: true, style:'font-weight: 600;', totalRow: true},
            {field: 'shifacheci', title: '实发车次', sort: true, style:'font-weight: 600;', totalRow: true},

            {field: 'zongtiji', title: '总体积', sort: true, style:'font-weight: 600;', totalRow: true},
            {field: 'zongjianshu', title: '总件数', sort: true, style:'font-weight: 600;', totalRow: true},
            {field: 'huansuanjianshu', title: '换算件数', sort: true, style:'font-weight: 600;', totalRow: true},
            {field: 'zonglicheng', title: '总里程', sort: true, style:'font-weight: 600;', totalRow: true},

            {field: 'junchetiji', title: '均车体积', sort: true, style:'font-weight: 600;'},
            {field: 'juncheshuliang', title: '均车数量', sort: true, style:'font-weight: 600;'},
            {field: 'junchelicheng', title: '均车里程', sort: true, style:'font-weight: 600;'},
        ]];
    };

    /**
     * 导出excel按钮
     */
    TDispatch.exportExcel = function () {
        var checkRows = table.checkStatus(TDispatch.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 导出excel
    $('#btnExp').click(function () {
        TDispatch.exportExcel();
    });

    /**
     * 点击查询按钮
     */
    TDispatch.search = function () {
        var queryData = {};
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['operateId'] = $("#operateId").val();
        queryData['carrierId'] = $("#carrierId").val();
        queryData['operateName'] = $("#operateName").val();
        queryData['carrierName'] = $("#carrierName").val();
        table.reload(TDispatch.tableId, {
            where: queryData, page: {curr: 1},
			done: function (res, curr, count) {
			}
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TDispatch.tableId,
        url: Feng.ctxPath + '/tDispatch/hxchengyunshanganalysis',
        page: false,
        limit: 1000,
        height: "full-60",
        totalRow: true,
        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        defaultToolbar: ['filter', 'exports', 'print'],
        cellMinWidth: 100,
        cols: TDispatch.initColumn(),
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TDispatch.search();
    });

    // 单击行事件
    table.on('row(' + TDispatch.tableId + ')', function (obj) {
        var data = obj.data;
		$('div[lay-id="' + TDispatch.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + TDispatch.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
    });

    //查询库区
    form.on('select(operateId)',function(){
        $("#carrierId").empty();
        $.ajax({
            url:Feng.ctxPath + '/basicCarrier/Querylist',
            dataType:"json",   //返回格式为json
            async: false,
            data:{"operateId":$("#operateId").val()},    //参数值
            type:"post",   //请求方式
            success:function(data){
                if(data.length>0){
                    var str = "<option value=''>选择承运商</option>";
                    $.each(data,function(key,value){
                        str += "<option value='"+value.id+"'>"+value.carrierName+"</option>";
                    });
                    $("#carrierId").html(str);
                }
            }
        });
        form.render('select');
    });

});
